package net.alan.modules.service.impl;

import net.alan.common.utils.CompareUtil;
import net.alan.modules.bean.*;
import net.alan.modules.repository.NoteCatalogRepository;
import net.alan.modules.repository.NoteHistoryRepository;
import net.alan.modules.repository.NoteRepository;
import net.alan.modules.service.INoteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/21
 */
@Service
@Transactional
public class NoteServiceImpl extends BaseServiceImpl implements INoteService {
    private static final Logger logger = LoggerFactory.getLogger(NoteServiceImpl.class);
    @Autowired
    private NoteRepository noteRepository;
    @Autowired
    private NoteCatalogRepository noteCatalogRepository;
    @Autowired
    private NoteHistoryRepository noteHistoryRepository;

    @Override
    public JpaRepository getJpaRepository(Class<?> cla) {
        if (isSame(cla, Note.class)) {
            return noteRepository;
        } else if (isSame(cla, NoteCatalog.class)) {
            return noteCatalogRepository;
        } else if (isSame(cla, NoteHistory.class)) {
            return noteHistoryRepository;
        }
        return null;
    }

    @Override
    public Page<Note> findNoteByUserId(Integer userId, PageModel pageModel) {
        return this.noteRepository.findByUserId(userId, pageModel.getPageable());
    }

    @Override
    public Page<Note> findByNoteUserIdAndCatalogId(Integer userId, Integer catalogId, PageModel pageModel) {
        return this.noteRepository.findByUserIdAndCatalogId(userId, catalogId, pageModel.getPageable());
    }

    @Override
    public Page<NoteHistory> findNoteHistoryByNoteId(Integer userId, Integer noteId, PageModel pageModel) {
        return this.noteHistoryRepository.findByUserIdAndNoteId(userId, noteId, pageModel.getPageable());
    }

    @Override
    public List<NoteCatalog> findNoteCatalogByUserId(Integer userId) {
        return this.noteCatalogRepository.findByUserId(userId);
    }

    @Override
    public Result saveNote(String content, Note note) {
        try {
            note.setContent(CompareUtil.wrapHtml(note.getContent()));
            Integer noteId = note.getId();
            if (noteId != null && noteId > 0) {
                NoteHistory noteHistory = new NoteHistory();
                noteHistory.setUserId(note.getUserId());
                noteHistory.setContent(content);
                noteHistory.setName(note.getName());
                noteHistory.setNoteId(noteId);
                noteHistory.setCreated(note.getModified());
                noteHistory.setVersion(note.getVersion());
                this.noteHistoryRepository.save(noteHistory);
            }
            note.setVersion(note.getVersion() + 1);
            note.setModified(new Date());
            this.noteRepository.save(note);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("错误");
        }
        return ResultUtil.success();
    }

    @Override
    public Result deleteNote(Integer id) {
        this.noteRepository.delete(id);
        this.noteHistoryRepository.deleteByNoteId(id);
        return ResultUtil.success();
    }

    @Override
    public Result deleteCatalog(Integer id) {
        int count = this.noteRepository.countByCatalogId(id);
        if (count > 0) return ResultUtil.error("该目录下有笔记，不能删除");
        this.noteCatalogRepository.delete(id);
        return ResultUtil.success();
    }

    @Override
    public Note getNoteByShareId(String shareId) {
        return this.noteRepository.getByShareId(shareId);
    }
}
